{
 "nbformat": 4,
 "nbformat_minor": 0,
 "metadata": {
  "colab": {
   "name": "train_demo.ipynb",
   "provenance": [],
   "collapsed_sections": []
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "accelerator": "GPU"
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "UWNk8sIT8I_c"
   },
   "source": [
    "This demo run successful in goolge colab for person detection, for time reason, only run 20 epochs."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "jgZ1tAlf5U00"
   },
   "source": [
    "\n",
    "download code!"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "E6E2hqMpx_r2",
    "outputId": "1279f3a1-4c81-40d4-ffbd-d5a3b4a7bea7"
   },
   "source": [
    "!git clone https://github.com/yl305237731/flexible-yolov5.git"
   ],
   "execution_count": 1,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "Cloning into 'flexible-yolov5'...\n",
      "remote: Enumerating objects: 481, done.\u001B[K\n",
      "remote: Counting objects: 100% (481/481), done.\u001B[K\n",
      "remote: Compressing objects: 100% (341/341), done.\u001B[K\n",
      "remote: Total 481 (delta 249), reused 323 (delta 115), pack-reused 0\u001B[K\n",
      "Receiving objects: 100% (481/481), 1.44 MiB | 29.41 MiB/s, done.\n",
      "Resolving deltas: 100% (249/249), done.\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "-79Rh8AX5bGD"
   },
   "source": [
    "download dataset!"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "ouBvhHPNyek8",
    "outputId": "632afcad-e190-4342-a1b2-cc2aa84acac7"
   },
   "source": [
    "!wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar\n",
    "\n",
    "!tar -xf VOCtrainval_11-May-2012.tar"
   ],
   "execution_count": 2,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "--2021-07-20 07:31:08--  http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar\n",
      "Resolving host.robots.ox.ac.uk (host.robots.ox.ac.uk)... 129.67.94.152\n",
      "Connecting to host.robots.ox.ac.uk (host.robots.ox.ac.uk)|129.67.94.152|:80... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 1999639040 (1.9G) [application/x-tar]\n",
      "Saving to: ‘VOCtrainval_11-May-2012.tar’\n",
      "\n",
      "VOCtrainval_11-May- 100%[===================>]   1.86G  14.9MB/s    in 2m 8s   \n",
      "\n",
      "2021-07-20 07:33:16 (14.9 MB/s) - ‘VOCtrainval_11-May-2012.tar’ saved [1999639040/1999639040]\n",
      "\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "JsseRroK8TtU"
   },
   "source": [
    "install python requirements"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "AjcbaLit0iGd",
    "outputId": "a15e12c8-61e7-4b04-88c7-9948057be9b8"
   },
   "source": [
    "!pip install -r flexible-yolov5/requirements.txt"
   ],
   "execution_count": 16,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: Cython in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 4)) (0.29.23)\n",
      "Requirement already satisfied: matplotlib>=3.2.2 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 5)) (3.2.2)\n",
      "Requirement already satisfied: numpy>=1.18.5 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 6)) (1.19.5)\n",
      "Requirement already satisfied: opencv-python>=4.1.2 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 7)) (4.1.2.30)\n",
      "Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 8)) (7.1.2)\n",
      "Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 9)) (5.4.1)\n",
      "Requirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 10)) (1.4.1)\n",
      "Requirement already satisfied: tensorboard>=2.2 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 11)) (2.5.0)\n",
      "Requirement already satisfied: torch>=1.7.0 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 12)) (1.9.0+cu102)\n",
      "Requirement already satisfied: torchvision>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 13)) (0.10.0+cu102)\n",
      "Requirement already satisfied: tqdm>=4.41.0 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 14)) (4.41.1)\n",
      "Requirement already satisfied: timm in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 15)) (0.4.12)\n",
      "Collecting addict\n",
      "  Downloading addict-2.4.0-py3-none-any.whl (3.8 kB)\n",
      "Requirement already satisfied: wandb in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 18)) (0.10.33)\n",
      "Requirement already satisfied: seaborn>=0.11.0 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 21)) (0.11.1)\n",
      "Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 22)) (1.1.5)\n",
      "Requirement already satisfied: coremltools>=4.1 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 25)) (4.1)\n",
      "Requirement already satisfied: onnx>=1.8.1 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 26)) (1.9.0)\n",
      "Requirement already satisfied: scikit-learn==0.19.2 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 27)) (0.19.2)\n",
      "Requirement already satisfied: thop in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 30)) (0.0.31.post2005241907)\n",
      "Requirement already satisfied: pycocotools>=2.0 in /usr/local/lib/python3.7/dist-packages (from -r flexible-yolov5/requirements.txt (line 31)) (2.0.2)\n",
      "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=3.2.2->-r flexible-yolov5/requirements.txt (line 5)) (2.4.7)\n",
      "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=3.2.2->-r flexible-yolov5/requirements.txt (line 5)) (0.10.0)\n",
      "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=3.2.2->-r flexible-yolov5/requirements.txt (line 5)) (2.8.1)\n",
      "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=3.2.2->-r flexible-yolov5/requirements.txt (line 5)) (1.3.1)\n",
      "Requirement already satisfied: setuptools>=41.0.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (57.2.0)\n",
      "Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (0.6.1)\n",
      "Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (3.17.3)\n",
      "Requirement already satisfied: absl-py>=0.4 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (0.12.0)\n",
      "Requirement already satisfied: grpcio>=1.24.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (1.34.1)\n",
      "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (1.8.0)\n",
      "Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (1.0.1)\n",
      "Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (1.32.1)\n",
      "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (0.4.4)\n",
      "Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (3.3.4)\n",
      "Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (0.36.2)\n",
      "Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.7/dist-packages (from tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (2.23.0)\n",
      "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from torch>=1.7.0->-r flexible-yolov5/requirements.txt (line 12)) (3.7.4.3)\n",
      "Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas->-r flexible-yolov5/requirements.txt (line 22)) (2018.9)\n",
      "Requirement already satisfied: attrs in /usr/local/lib/python3.7/dist-packages (from coremltools>=4.1->-r flexible-yolov5/requirements.txt (line 25)) (21.2.0)\n",
      "Requirement already satisfied: sympy in /usr/local/lib/python3.7/dist-packages (from coremltools>=4.1->-r flexible-yolov5/requirements.txt (line 25)) (1.7.1)\n",
      "Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from coremltools>=4.1->-r flexible-yolov5/requirements.txt (line 25)) (21.0)\n",
      "Requirement already satisfied: attr in /usr/local/lib/python3.7/dist-packages (from coremltools>=4.1->-r flexible-yolov5/requirements.txt (line 25)) (0.3.1)\n",
      "Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python3.7/dist-packages (from coremltools>=4.1->-r flexible-yolov5/requirements.txt (line 25)) (1.15.0)\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (4.7.2)\n",
      "Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (4.2.2)\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.7/dist-packages (from google-auth<2,>=1.6.3->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (0.2.8)\n",
      "Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.7/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (1.3.0)\n",
      "Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from markdown>=2.6.8->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (4.6.1)\n",
      "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.7/dist-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (0.4.8)\n",
      "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (1.24.3)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (2021.5.30)\n",
      "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (3.0.4)\n",
      "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.21.0->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (2.10)\n",
      "Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.7/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (3.1.1)\n",
      "Requirement already satisfied: shortuuid>=0.5.0 in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (1.0.1)\n",
      "Requirement already satisfied: promise<3,>=2.0 in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (2.3)\n",
      "Requirement already satisfied: pathtools in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (0.1.2)\n",
      "Requirement already satisfied: subprocess32>=3.5.3 in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (3.5.4)\n",
      "Requirement already satisfied: configparser>=3.8.1 in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (5.0.2)\n",
      "Requirement already satisfied: sentry-sdk>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (1.3.0)\n",
      "Requirement already satisfied: psutil>=5.0.0 in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (5.4.8)\n",
      "Requirement already satisfied: GitPython>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (3.1.18)\n",
      "Requirement already satisfied: docker-pycreds>=0.4.0 in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (0.4.0)\n",
      "Requirement already satisfied: Click!=8.0.0,>=7.0 in /usr/local/lib/python3.7/dist-packages (from wandb->-r flexible-yolov5/requirements.txt (line 18)) (7.1.2)\n",
      "Requirement already satisfied: gitdb<5,>=4.0.1 in /usr/local/lib/python3.7/dist-packages (from GitPython>=1.0.0->wandb->-r flexible-yolov5/requirements.txt (line 18)) (4.0.7)\n",
      "Requirement already satisfied: smmap<5,>=3.0.1 in /usr/local/lib/python3.7/dist-packages (from gitdb<5,>=4.0.1->GitPython>=1.0.0->wandb->-r flexible-yolov5/requirements.txt (line 18)) (4.0.0)\n",
      "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata->markdown>=2.6.8->tensorboard>=2.2->-r flexible-yolov5/requirements.txt (line 11)) (3.5.0)\n",
      "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.7/dist-packages (from sympy->coremltools>=4.1->-r flexible-yolov5/requirements.txt (line 25)) (1.2.1)\n",
      "Installing collected packages: addict\n",
      "Successfully installed addict-2.4.0\n"
     ],
     "name": "stdout"
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "wST0bslH5i2-"
   },
   "source": [
    "Make data for yolov5 format.\n",
    "I only training for person detection.\n",
    "\n",
    "This script in od/data/transform_voc.py"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "QjMs2g8X084S",
    "outputId": "7a2c677a-abc6-49e7-ca75-4a033b328661"
   },
   "source": [
    "import xml.etree.ElementTree as ET\n",
    "import os\n",
    "import shutil\n",
    "\n",
    "global year\n",
    "year = '2012'\n",
    "\n",
    "\n",
    "def convert(size, box):\n",
    "    dw = 1. / (size[0])\n",
    "    dh = 1. / (size[1])\n",
    "    x = max((box[0] + box[1]) / 2.0 - 1, 0)\n",
    "    y = max((box[2] + box[3]) / 2.0 - 1, 0)\n",
    "    w = box[1] - box[0]\n",
    "    h = box[3] - box[2]\n",
    "    x = x * dw\n",
    "    w = w * dw\n",
    "    y = y * dh\n",
    "    h = h * dh\n",
    "    return (x, y, w, h)\n",
    "\n",
    "\n",
    "def convert_annotation(data_dir, image_id, train, classes):\n",
    "    in_file = open(os.path.join(data_dir, 'VOC' + year + '/Annotations/%s.xml' % (image_id)), encoding='utf-8')\n",
    "    if train:\n",
    "        out_file = open(os.path.join(data_dir, 'labels/train/%s.txt' % (image_id)), 'w', encoding='utf-8')\n",
    "    else:\n",
    "        out_file = open(os.path.join(data_dir, 'labels/val/%s.txt' % (image_id)), 'w', encoding='utf-8')\n",
    "    tree = ET.parse(in_file)\n",
    "    root = tree.getroot()\n",
    "    size = root.find('size')\n",
    "    w = int(size.find('width').text)\n",
    "    h = int(size.find('height').text)\n",
    "\n",
    "    for obj in root.iter('object'):\n",
    "        difficult = obj.find('difficult').text\n",
    "        cls = obj.find('name').text\n",
    "        if cls not in classes:\n",
    "          continue\n",
    "        cls_id = classes.index(cls)\n",
    "        xmlbox = obj.find('bndbox')\n",
    "        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),\n",
    "             float(xmlbox.find('ymax').text))\n",
    "        bb = convert((w, h), b)\n",
    "        out_file.write(str(cls_id) + \" \" + \" \".join([str(a) for a in bb]) + '\\n')\n",
    "\n",
    "\n",
    "def transform_voc(data_dir, classes, c_year=None):\n",
    "    if c_year is not None:\n",
    "        year = c_year\n",
    "\n",
    "    if not os.path.exists(os.path.join(data_dir, 'images')):\n",
    "        os.makedirs(os.path.join(data_dir, 'images/train'))\n",
    "        os.makedirs(os.path.join(data_dir, 'images/val'))\n",
    "    if not os.path.exists(os.path.join(data_dir, 'labels')):\n",
    "        os.makedirs(os.path.join(data_dir, 'labels/train'))\n",
    "        os.makedirs(os.path.join(data_dir, 'labels/val'))\n",
    "\n",
    "    # make train labels\n",
    "    train_image_ids = open(os.path.join(data_dir, 'VOC' + year + '/ImageSets/Main/train.txt'), encoding='utf-8')\n",
    "    for image_id in train_image_ids:\n",
    "        image_id = image_id.strip()\n",
    "        convert_annotation(data_dir, image_id, True, classes)\n",
    "        img_path = os.path.join(data_dir, 'VOC' + year + \"/JPEGImages\", image_id + '.jpg')\n",
    "        shutil.copy(img_path, os.path.join(data_dir, 'images/train/'))\n",
    "\n",
    "    # make val labels\n",
    "    val_image_ids = open(os.path.join(data_dir, 'VOC' + year + '/ImageSets/Main/val.txt'), encoding='utf-8')\n",
    "    for image_id in val_image_ids:\n",
    "        image_id = image_id.strip()\n",
    "        convert_annotation(data_dir, image_id, False, classes)\n",
    "        img_path = os.path.join(data_dir, 'VOC' + year + \"/JPEGImages\", image_id + '.jpg')\n",
    "        shutil.copy(img_path, os.path.join(data_dir, 'images/val/'))\n",
    "\n",
    "    return os.path.join(data_dir, 'images/train'), os.path.join(data_dir, 'images/val')\n",
    "\n",
    "\n",
    "data_dir = './VOCdevkit'\n",
    "classes = ['person'] # detection class\n",
    "transform_voc(data_dir, classes, year)\n"
   ],
   "execution_count": 25,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "('./VOCdevkit/images/train', './VOCdevkit/images/val')"
      ]
     },
     "metadata": {
      "tags": []
     },
     "execution_count": 25
    }
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "vqH0FleC555P"
   },
   "source": [
    "Change configs/data.yaml\n",
    "\n",
    "1. set your dataset path\n",
    "2. change number of classes\n",
    "3. set names, for visualization."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "PL0awxzY3D6V"
   },
   "source": [
    "# Custom data for person detection, configs/data/yaml\n",
    "\n",
    "\n",
    "# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]\n",
    "train: /content/VOCdevkit/images/train/\n",
    "val: /content/VOCdevkit/images/val/\n",
    "\n",
    "# number of classes\n",
    "nc: 1\n",
    "\n",
    "# class names\n",
    "names: ['person']\n"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "j6vWDjVT6Z2n"
   },
   "source": [
    "Here we use yolov5s, change the nc to your number of classes. only one class, set nc=1. configs/model_yolo.yaml\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "id": "SJkjZ26J3iNh"
   },
   "source": [
    "backbone:\n",
    "  type: YOLOv5\n",
    "  version: s # x, m, l ,x\n",
    "  focus: True\n",
    "  with_C3TR: False\n",
    "head:\n",
    "  nc: 1\n",
    "  stride: [8.0, 16.0, 32.0]\n",
    "  anchors:\n",
    "    - [10,13, 16,30, 33,23]  # P3/8\n",
    "    - [30,61, 62,45, 59,119]  # P4/16\n",
    "    - [116,90, 156,198, 373,326]  # P5/32"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5YYB0vOi8xQl"
   },
   "source": [
    "Start training and use default parameters."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "O2Y8EM7b3og3",
    "outputId": "4689b776-99b4-43f7-81a6-855cf3c6969b"
   },
   "source": [
    "!cd flexible-yolov5/ && python scripts/train.py"
   ],
   "execution_count": null,
   "outputs": [
    {
     "output_type": "stream",
     "text": [
      "fatal: ambiguous argument 'main..origin/master': unknown revision or path not in the working tree.\n",
      "Use '--' to separate paths from revisions, like this:\n",
      "'git <command> [<revision>...] -- [<file>...]'\n",
      "\u001B[34m\u001B[1mgithub: \u001B[0mCommand 'git rev-list main..origin/master --count' returned non-zero exit status 128.\n",
      "YOLOv5 2e0b9bf torch 1.9.0+cu102 CUDA:0 (Tesla T4, 15109.75MB)\n",
      "\n",
      "Namespace(adam=False, batch_size=32, bucket='', cache_images=False, cfg='configs/model_yolo.yaml', data='configs/data.yaml', device='', epochs=300, evolve=False, exist_ok=False, global_rank=-1, hyp='configs/hyp.scratch.yaml', image_weights=False, img_size=[640, 640], linear_lr=False, local_rank=-1, log_artifacts=False, log_imgs=16, multi_scale=False, name='exp', noautoanchor=False, nosave=False, notest=False, project='runs/train', quad=False, rect=False, resume=False, save_dir='runs/train/exp6', single_cls=False, sync_bn=False, total_batch_size=32, weights='', workers=8, world_size=1)\n",
      "Start Tensorboard with \"tensorboard --logdir runs/train\", view at http://localhost:6006/\n",
      "2021-07-20 09:48:23.971641: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0\n",
      "\u001B[34m\u001B[1mhyperparameters: \u001B[0mlr0=0.01, lrf=0.2, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.5, cls_pw=1.0, obj=1.0, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0\n",
      "./od/models/model.py:22: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.\n",
      "  model_config = yaml.load(open(model_config, 'r'))\n",
      "backbone output channel: C3 128, C4 256, C5 256\n",
      "FPN input channel size: C3 128, C4 256, C5 256\n",
      "FPN output channel size: P3 384, P4 256, P5 256\n",
      "PAN input channel size: P3 384, P4 256, P5 256\n",
      "PAN output channel size: PP3 128, PP4 256, PP5 512\n",
      "Scaled weight_decay = 0.0005\n",
      "Optimizer groups: 66 .bias, 66 conv.weight, 63 other\n",
      "\u001B[34m\u001B[1mwandb\u001B[0m: (1) Create a W&B account\n",
      "\u001B[34m\u001B[1mwandb\u001B[0m: (2) Use an existing W&B account\n",
      "\u001B[34m\u001B[1mwandb\u001B[0m: (3) Don't visualize my results\n",
      "\u001B[34m\u001B[1mwandb\u001B[0m: Enter your choice: 3\n",
      "\u001B[34m\u001B[1mwandb\u001B[0m: You chose 'Don't visualize my results'\n",
      "2021-07-20 09:48:33.494496: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcudart.so.11.0\n",
      "\u001B[34m\u001B[1mwandb\u001B[0m: W&B syncing is set to `offline` in this directory.  Run `wandb online` or set WANDB_MODE=online to enable cloud syncing.\n",
      "\u001B[34m\u001B[1mtrain: \u001B[0mScanning '/content/VOCdevkit/labels/train' for images and labels... 5717 found, 0 missing, 3575 empty, 0 corrupted: 100% 5717/5717 [00:01<00:00, 4651.29it/s]\n",
      "\u001B[34m\u001B[1mtrain: \u001B[0mNew cache created: /content/VOCdevkit/labels/train.cache\n",
      "\u001B[34m\u001B[1mval: \u001B[0mScanning '/content/VOCdevkit/labels/val' for images and labels... 5823 found, 0 missing, 3591 empty, 0 corrupted: 100% 5823/5823 [00:03<00:00, 1795.91it/s]\n",
      "\u001B[34m\u001B[1mval: \u001B[0mNew cache created: /content/VOCdevkit/labels/val.cache\n",
      "[W pthreadpool-cpp.cc:90] Warning: Leaking Caffe2 thread-pool after fork. (function pthreadpool)\n",
      "[W pthreadpool-cpp.cc:90] Warning: Leaking Caffe2 thread-pool after fork. (function pthreadpool)\n",
      "Plotting labels... \n",
      "\n",
      "\u001B[34m\u001B[1mautoanchor: \u001B[0mAnalyzing anchors... anchors/target = 4.18, Best Possible Recall (BPR) = 1.0000\n",
      "Image sizes 640 train, 640 test\n",
      "Using 2 dataloader workers\n",
      "Logging results to runs/train/exp6\n",
      "Starting training for 300 epochs...\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "  0% 0/179 [00:00<?, ?it/s]/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /pytorch/c10/core/TensorImpl.h:1156.)\n",
      "  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)\n",
      "     0/299     7.44G   0.09509   0.01951         0    0.1146        33       640: 100% 179/179 [03:18<00:00,  1.11s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [01:36<00:00,  1.06s/it]\n",
      "                 all    5.82e+03    5.11e+03     0.00513      0.0485      0.0014    0.000206\n",
      "Images sizes do not match. This will causes images to be display incorrectly in the UI.\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "     1/299     7.45G   0.09025   0.01835         0    0.1086        59       640: 100% 179/179 [03:08<00:00,  1.06s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:55<00:00,  1.65it/s]\n",
      "                 all    5.82e+03    5.11e+03      0.0178       0.018     0.00341    0.000685\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "     2/299     7.45G   0.08762   0.01888         0    0.1065        35       640: 100% 179/179 [03:13<00:00,  1.08s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:54<00:00,  1.68it/s]\n",
      "                 all    5.82e+03    5.11e+03      0.0352       0.119     0.00993     0.00163\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "     3/299     7.45G   0.08331    0.0191         0    0.1024        28       640: 100% 179/179 [03:08<00:00,  1.05s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:56<00:00,  1.60it/s]\n",
      "                 all    5.82e+03    5.11e+03      0.0184      0.0395     0.00346    0.000535\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "     4/299     7.45G    0.0801   0.01883         0   0.09893        33       640: 100% 179/179 [03:05<00:00,  1.03s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:56<00:00,  1.61it/s]\n",
      "                 all    5.82e+03    5.11e+03     0.00538      0.0305     0.00162    0.000262\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "     5/299     7.45G   0.07646   0.01908         0   0.09553        33       640: 100% 179/179 [03:10<00:00,  1.06s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:57<00:00,  1.59it/s]\n",
      "                 all    5.82e+03    5.11e+03      0.0319      0.0677      0.0114      0.0025\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "     6/299     7.45G   0.07232   0.01874         0   0.09106        30       640: 100% 179/179 [03:09<00:00,  1.06s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:55<00:00,  1.63it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.104       0.161      0.0521       0.012\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "     7/299     7.45G   0.06982   0.01942         0   0.08925        39       640: 100% 179/179 [03:09<00:00,  1.06s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:56<00:00,  1.61it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.105       0.138      0.0461       0.011\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "     8/299     7.45G   0.06797   0.01858         0   0.08656        39       640: 100% 179/179 [03:07<00:00,  1.05s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:55<00:00,  1.65it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.173       0.193      0.0936      0.0249\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "     9/299     7.45G    0.0654   0.01898         0   0.08439        35       640: 100% 179/179 [03:06<00:00,  1.04s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:55<00:00,  1.64it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.157       0.147      0.0815      0.0208\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    10/299     7.45G   0.06444   0.01806         0    0.0825        24       640: 100% 179/179 [03:06<00:00,  1.04s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:54<00:00,  1.67it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.182       0.163      0.0884      0.0218\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    11/299     7.45G   0.06375   0.01823         0   0.08198        35       640: 100% 179/179 [03:06<00:00,  1.04s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:52<00:00,  1.73it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.241        0.25       0.156      0.0449\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    12/299     7.45G   0.06205   0.01756         0   0.07961        31       640: 100% 179/179 [03:07<00:00,  1.05s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:52<00:00,  1.74it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.318       0.272       0.214       0.064\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    13/299     7.45G   0.06132   0.01766         0   0.07898        35       640: 100% 179/179 [03:03<00:00,  1.03s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:53<00:00,  1.69it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.359       0.335       0.266      0.0834\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    14/299     7.45G   0.06002    0.0173         0   0.07731        45       640: 100% 179/179 [03:00<00:00,  1.01s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:56<00:00,  1.61it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.293       0.311       0.221      0.0652\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    15/299     7.45G   0.05886    0.0173         0   0.07616        27       640: 100% 179/179 [03:12<00:00,  1.08s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:54<00:00,  1.68it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.341       0.314       0.243       0.076\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    16/299     7.45G   0.05816   0.01719         0   0.07536        33       640: 100% 179/179 [03:08<00:00,  1.05s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:53<00:00,  1.69it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.447       0.343       0.335        0.12\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    17/299     7.45G   0.05681   0.01697         0   0.07378        28       640: 100% 179/179 [03:08<00:00,  1.05s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:53<00:00,  1.69it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.392       0.354       0.304       0.102\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    18/299     7.45G   0.05711    0.0172         0   0.07431        27       640: 100% 179/179 [03:09<00:00,  1.06s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:55<00:00,  1.65it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.413       0.329       0.295      0.0971\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    19/299     7.45G   0.05601   0.01684         0   0.07285        25       640: 100% 179/179 [03:09<00:00,  1.06s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:56<00:00,  1.60it/s]\n",
      "                 all    5.82e+03    5.11e+03        0.47       0.324       0.329       0.116\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    20/299     7.45G   0.05583    0.0164         0   0.07222        26       640: 100% 179/179 [03:11<00:00,  1.07s/it]\n",
      "               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 91/91 [00:55<00:00,  1.64it/s]\n",
      "                 all    5.82e+03    5.11e+03       0.332       0.322       0.256      0.0892\n",
      "\n",
      "     Epoch   gpu_mem       box       obj       cls     total   targets  img_size\n",
      "    21/299     7.45G   0.05504   0.01762         0   0.07266        59       640:  31% 55/179 [00:56<02:00,  1.03it/s]"
     ],
     "name": "stdout"
    }
   ]
  }
 ]
}